package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.util.DateUtil;
import cn.stylefeng.guns.sys.modular.system.entity.CompanyInfo;
import cn.stylefeng.guns.sys.modular.system.mapper.CompanyInfoMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.CompanyInfoParam;
import cn.stylefeng.guns.sys.modular.system.model.result.CompanyInfoResult;
import cn.stylefeng.guns.sys.modular.system.service.CompanyInfoService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiangge
 * @since 2020-09-16
 */
@Service
public class CompanyInfoServiceImpl extends ServiceImpl<CompanyInfoMapper, CompanyInfo> implements CompanyInfoService {
    private Logger logger = LoggerFactory.getLogger(CompanyInfoServiceImpl.class);
    private RestTemplate restTemplate = new RestTemplate();
    @Override
    public void add(CompanyInfoParam param){
        CompanyInfo entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(CompanyInfoParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(CompanyInfoParam param){
        CompanyInfo oldEntity = getOldEntity(param);
        CompanyInfo newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public CompanyInfoResult findBySpec(CompanyInfoParam param){
        return null;
    }

    @Override
    public List<CompanyInfoResult> findListBySpec(CompanyInfoParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(CompanyInfoParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public void initCompanyInfoData() {
        String url = "http://btyjt.c3.fxscm.net/cldpoint/getCompanyInfo.do?ent=ENT7617&username=admin&password=admin123";
        String content = restTemplate.getForObject(url, String.class);
        JSONObject resultObject = JSON.parseObject(content);
        JSONArray jsonArray = resultObject.getJSONArray("data");

        JSONObject jsonObject;
        List<CompanyInfoParam> list = Lists.newLinkedList();
        logger.info("分公司档案数据入库开始：{}条数据", list.size());
        if (jsonArray != null) {
            for (int i = 0; i < jsonArray.size(); i++) {
                CompanyInfoParam companyInfo = new CompanyInfoParam();
                //把所有 ItemSmallClassName 为 酱料 的添加到list中
                jsonObject = jsonArray.getJSONObject(i);

                companyInfo.setId(jsonObject.getOrDefault("id", "").toString());
                companyInfo.setCode(jsonObject.getOrDefault("code", "").toString());
                companyInfo.setName(jsonObject.getOrDefault("name", "").toString());
                if(jsonObject.getOrDefault("enableFlag", "").toString().equals("")){
                    companyInfo.setEnableFlag(null);
                }else {
                    companyInfo.setEnableFlag(Integer.valueOf(jsonObject.getOrDefault("enableFlag", "").toString()));
                }
                Date modifyDate = DateUtil.getTimeToDate(jsonObject.getOrDefault("modifyDate", "").toString());
                companyInfo.setModifyDate(modifyDate);
                companyInfo.setParentId(jsonObject.getOrDefault("parentid", "").toString());
                list.add(companyInfo);
            }
        }

        //如果有数据则清空店铺表再插入，以免之前的数据也丢失了
        if(list.size()>0){
            deleteAllCompanyInfo();
        }

        //插入数据
        for (CompanyInfoParam companyInfoParam : list) {
            add(companyInfoParam);
        }

        logger.info("分公司档案数据入库结束：{}条数据", list.size());
    }

    private void deleteAllCompanyInfo() {
        this.baseMapper.deleteAllCompanyInfo();

    }

    private Serializable getKey(CompanyInfoParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private CompanyInfo getOldEntity(CompanyInfoParam param) {
        return this.getById(getKey(param));
    }

    private CompanyInfo getEntity(CompanyInfoParam param) {
        CompanyInfo entity = new CompanyInfo();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
